//jq的入口函数 当页面的dom树创建完成时调用
$(function() {
    getUserInfo()
    $('#btnLogout').on('click', logout)
})

function getUserInfo() {
    $.ajax({
        // method: 'GET',
        url: '/my/userinfo',
        success(res) {
            layui.layer.msg(res.message)
            if (res.status !== 0) {
                return
            }

            render(res.data)
        }

    })
}

function render(usrInfo) {
    const usrName = usrInfo.nickname || usrInfo.username
    $('#usrName').text(usrName)
    if (usrInfo.user_pic) {
        $('.img-avatar').attr('src', usrInfo.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.text-avatar').text(usrName[0].toUpperCase()).show()
        $('.img-avatar').hide()
    }
}

function logout() {
    layui.layer.confirm('确定么', { icon: 3, title: '艹' }, function(index) {
        localStorage.removeItem('token')
        location.href = "/login.html"
    })
}